package com.test.webdemo.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class TrackingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {
        RequestTracker.log("Filter initialized: " + this.getClass().getSimpleName());
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        RequestTracker.logStep("1. Filter开始 - URI: " + httpRequest.getRequestURI());

        long startTime = System.currentTimeMillis();
        chain.doFilter(request, response);
        long duration = System.currentTimeMillis() - startTime;

        RequestTracker.logStep("12. Filter结束 - 耗时: " + duration + "ms");
    }

    @Override
    public void destroy() {
        RequestTracker.log("Filter destroyed: " + this.getClass().getSimpleName());
    }
}
